package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsBorrcertificateDao;
import io.renren.modules.app.entity.BsBorrcertificateEntity;
import io.renren.modules.app.service.BsBorrcertificateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;


@Service("bsBorrcertificateService")
@Slf4j
public class BsBorrcertificateServiceImpl extends ServiceImpl<BsBorrcertificateDao, BsBorrcertificateEntity> implements BsBorrcertificateService {

    @Resource
    private BsBorrcertificateDao bsBorrcertificateDao ;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<BsBorrcertificateEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (params.get("certnum") != null && !params.get("certnum").equals("")){
            queryWrapper.like(BsBorrcertificateEntity::getCertnum,params.get("certnum"));
        }
        if (params.get("certname") != null && !params.get("certname").equals("")){
            queryWrapper.like(BsBorrcertificateEntity::getCertname,params.get("certname"));
        }
        if (params.get("certsex") != null && !params.get("certsex").equals("")){
            queryWrapper.eq(BsBorrcertificateEntity::getCertsex,params.get("certsex"));
        }
        if (params.get("certaddress") != null && !params.get("certaddress").equals("")){
            queryWrapper.like(BsBorrcertificateEntity::getCertaddress,params.get("certaddress"));
        }
        if (params.get("certstatus") != null && !params.get("certstatus").equals("")){
            queryWrapper.eq(BsBorrcertificateEntity::getCertstatus,params.get("certstatus"));
        }
        IPage<BsBorrcertificateEntity> page = this.page(
                new Query<BsBorrcertificateEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBsBorrcertificateEntity(BsBorrcertificateEntity bsBorrcertificate) {
        try {
            if (bsBorrcertificate.getCertidnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsBorrcertificate.getCertphone())){
                throw new RRException("电话号不合法");
            }
            //生成借阅证编号 年月日 + 身份证号
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date).replace("-","");
            String certnum = format + bsBorrcertificate.getCertidnum() ;
            bsBorrcertificate.setCertnum(certnum);
            bsBorrcertificateDao.insert(bsBorrcertificate);
        }catch (Exception e){
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void startBatch(Integer[] ids) {
        Integer num = bsBorrcertificateDao.startBatch(ids);
        System.out.println(num);
    }

    @Override
    public void stopBatch(Integer[] ids) {
        Integer num = bsBorrcertificateDao.stopBatch(ids);
        System.out.println(num);
    }

    @Override
    public void updateBsBorrcertificateEntityById(BsBorrcertificateEntity bsBorrcertificate) {
        if (bsBorrcertificate.getCertidnum().length() != 18){
            throw new RRException("身份证号长度不合法");
        }
        //电话号校验
        if (!phoneValidate(bsBorrcertificate.getCertphone())){
            throw new RRException("电话号不合法");
        }
        bsBorrcertificateDao.updateById(bsBorrcertificate);
    }

    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }

}
